$(document).ready(function () {

    ajax({
        url: "menu/getMenuList",
        type: "GET",
        dataType: 'jsonp',
        jsonp: "callback",
        async: true,
        data: {appKey:appKey},
        timeout: 8000,
        error: function (xhr, type) {
            layer.msg("服务器异常，请稍后重试！", {time:2000})
        },
        successx: function (data) {
            if (data.success == true) {
                createMainmenu(data.data);
            } else {
                layer.msg(data.message,{time:1000})
            }
        },
    });

})
//创建主菜单
function createMainmenu(data) {

    var temp = "";
    $.each(data, function (i, d) {
        temp += '<li id="' + d.resource_id + '" resource_request_uri="' + d.resource_request_uri + '">' +
            '<i class="fa ' + d.resource_image_uri + '"></i>' +
            '<span>' + d.resource_name + '</span>' +
            '</li>'
    })
    $(".mainNav").html(temp);
    $("#indexWrap #mainNav ul li").click(function () {
        if ($(this).hasClass('mainNavActive')) return;
        $("#indexWrap #mainNav ul li").removeClass('mainNavActive');
        $(this).addClass('mainNavActive');
        if (location.href.lastIndexOf('#') > 0) {
            location.href = location.href.split('#')[0] + "#" + $(this).attr('id') + "/";
        } else {
            location.href = location.href + "#" + $(this).attr('id') + '/'
        }
        if (isNULL($(this).attr('resource_request_uri'))) {
            initSecNav($(this).attr('id'));
        } else {
            $("#indexWrap #secNav").hide();
            $("#indexWrap #thirdNav").hide();
            $("#indexWrap .jitesLogo").css('width', 'calc(100% - 100px)')
            $("#mainFrame").css('left', '100px').css('top', '60px').css('width', 'calc(100% - 100px)').css('height', 'calc(100% - 56px)')
            $("#mainFrame iframe").attr('src', $(this).attr('resource_request_uri'));
        }
    })
    if (location.href.lastIndexOf('#') > 0) {
        $("#indexWrap #mainNav ul li").removeClass('mainNavActive');
        $("#" + location.href.split('#')[1].split('/')[0]).addClass('mainNavActive');
        if (isNULL( $("#" + location.href.split('#')[1].split('/')[0]).attr('resource_request_uri'))) {
            initSecNav(location.href.split('#')[1].split('/')[0]);
        } else {
            $("#indexWrap #secNav").hide();
            $("#indexWrap #thirdNav").hide();
            $("#indexWrap .jitesLogo").css('width', 'calc(100% - 100px)')
            $("#mainFrame").css('left', '100px').css('top', '60px').css('width', 'calc(100% - 100px)').css('height', 'calc(100% - 56px)')
            $("#mainFrame iframe").attr('src', $("#" + location.href.split('#')[1].split('/')[0]).attr('resource_request_uri'));
        }
    } else {
        $(".mainNav").find('li').eq(0).trigger("click");
    }

}
// 获取次级菜单
function initSecNav(id) {
    ajax({
        url: "menu/getMenuChildrenList",
        type: "GET",
        dataType: 'jsonp',
        jsonp: "callback",
        async: true,
        data: {
            resourceId:id,
            appKey:appKey
        },
        timeout: 8000,
        error: function (xhr, type) {
            layer.msg("服务器异常，请稍后重试！", {time:2000})
        },
        successx: function (data) {
            if (data.success == true) {
                creatMenu(data.data);
            } else {
                layer.msg(data.message,{time:1000})
            }
        },
    });
}
//加载次级菜单

function creatMenu(data) {
    var temp = "";
    $.each(data, function (i, d) {
        temp += '<li id="' + d.resource_id + '" resource_request_uri="' + d.resource_request_uri + '" i="' + i + '">' +
            '<span>' + d.resource_name + '</span>' +
            '</li>'
    });
    $("#indexWrap #secNav").show();
    $("#indexWrap .jitesLogo").css('width', 'calc(100% - 200px)')
    $("#mainFrame").css('left', '200px').css('top', '60px').css('width', 'calc(100% - 200px)').css('height', 'calc(100% - 56px)')
    $(".secNav").html(temp);
    $("#indexWrap #secNav ul li").click(function () {
        if ($(this).hasClass('secNavActive')) return;
        $("#indexWrap #secNav ul li").removeClass('secNavActive');
        $(this).addClass('secNavActive');
        location.href = location.href.split('#')[0] + "#" + $(".mainNavActive").attr('id') + "/" + $(this).attr('id') + '/';
        if (isNULL($(this).attr('resource_request_uri'))) {
            initThirdNav(data[$(this).attr('i')].nodes);
        } else {
            $("#indexWrap #thirdNav").hide();
            $("#indexWrap .jitesLogo").css('width', 'calc(100% - 200px)')
            $("#mainFrame").css('left', '200px')
                .css('top', '60px')
                // .css('width', 'calc(100% - 200px)')
                // .css('height', 'calc(100% - 56px)')
            $("#mainFrame iframe").attr('src', $(this).attr('resource_request_uri'));
        }
    });
    if (location.href.split('#')[1].split('/')[1] != '') {
        $("#indexWrap #secNav ul li").removeClass('secNavActive');
        $("#" + location.href.split('#')[1].split('/')[1]).addClass('secNavActive');
        if (isNULL($("#" + location.href.split('#')[1].split('/')[1]).attr('resource_request_uri'))) {
            // 加载第三级菜单
            // initThirdNav(data[$("#" + location.href.split('#')[1].split('/')[1]).attr('i')].nodes);
        } else {
            $("#indexWrap #thirdNav").hide();
            $("#indexWrap .jitesLogo").css('width', 'calc(100% - 200px)')
            $("#mainFrame").css('left', '200px')
                .css('top', '60px')
                .css('width', 'calc(100% - 200px)')
                .css('height', 'calc(100% - 56px)')
            $("#mainFrame iframe").attr('src', $("#" + location.href.split('#')[1].split('/')[1]).attr('resource_request_uri'));
        }

    } else {
        $(".secNav").find('li').eq(0).trigger("click");
    }
}
//加载三级菜单
function initThirdNav(data) {
    var temp = "";
    $.each(data, function (i, d) {
        temp += '<div class="swiper-slide" resource_request_uri="' + d.resource_request_uri + '" id="' +
            d.resource_id + '">' +
            '<p>' + d.resource_name + '</p>' +
            '</div>'
    })
    $(".thirdNav").html(temp);
    // var swiper = new Swiper('.swiper-container', {
    //     spaceBetween: 0,
    //     freeMode: false
    // });
    $("#indexWrap #thirdNav").show();
    $("#indexWrap .jitesLogo").css('width', 'calc(100% - 200px)')
    $("#mainFrame").css('left', '200px').css('top', '100px').css('width', 'calc(100% - 200px)').css('height', 'calc(100% - 150px)')
    $(".swiper-slide").click(function () {
        if ($(this).hasClass('thirdNavActive')) return;
        $(".swiper-slide").removeClass('thirdNavActive');
        $(this).addClass('thirdNavActive');
        location.href = location.href.split('#')[0] + "#" + $(".mainNavActive").attr('id') + "/" + $(".secNavActive").attr('id') + "/" + $(this).attr('id');
        $("#mainFrame iframe").attr('src', $(this).attr('resource_request_uri'));
    });
    if (location.href.split('#')[1].split('/')[2] != '') {
        $("#" + location.href.split('#')[1].split('/')[2]).trigger("click");
    } else {
        $(".thirdNav").find('.swiper-slide').eq(0).trigger("click");
    }

}


$('#changeAirport').on('click', function () {
    layer.open({
        type: 2,
        title: '切换机场',
        maxmin: true,
        shadeClose: true, //点击遮罩关闭层
        area: ['80%', '80%'],
        content: 'airportChange.html',
        end: function (index, layero) {}
    });
});
$('#changeShop').on('click', function () {
    setStorage("iframeUrl", $("#J_iframe").attr("src"))
    layer.open({
        type: 2,
        title: '切换店铺',
        maxmin: true,
        shadeClose: true, //点击遮罩关闭层
        area: ['80%', '80%'],
        content: 'shopChange.html',
        end: function (index, layero) {}
    });
});
$('#userInfordV').on('click', function () {
    layer.open({
        type: 2,
        title: '个人信息',
        maxmin: true,
        shadeClose: false, //点击遮罩关闭层
        area: ['500px', '410px'],
        closeBtn: 1,
        maxmin: false,
        content: 'userInfor.html'
    });
});
$('#passwordV').on('click', function () {
    layer.open({
        type: 2,
        title: '修改密码',
        maxmin: true,
        shadeClose: false, //点击遮罩关闭层
        closeBtn: 1,
        maxmin: false,
        area: ['500px', '400px'],
        content: 'password.html'
    });
});

$(".headeRight").click(function () {
    if ($(this).find("i").hasClass("fa-caret-down")) {
        $(this).find("i").removeClass("fa-caret-down").addClass("fa-caret-up");
    } else {
        $(this).find("i").removeClass("fa-caret-up").addClass("fa-caret-down");
    }
    $("#userInfor").slideToggle();
});
$('#logout').on('click', function () {
    window.location.href = web_url_prefix + "logout?redirect_uri=" + web_login_url;
});